<template>
	<view class="content" :style="{backgroundImage:`url(${indexBackgroundImage})`}">
		<view class="main">
			<view class="title">
				<view class="h1">
					<text>账号密码登录</text>
				</view>
				<view class="h2">
					<text>快加入我们吧~</text>
				</view>
			</view>
			<view class="login">
				<view class="tab">
				<!-- 	<view class="tab-1">
						<text class="active">登录</text>
					</view> -->
<!--					<view class="tab-1">-->
<!--						<text>注册</text>-->
<!--					</view>-->
				</view>
				<view class="tel">
					<image src="/static/login/tel.png" mode=""></image>
					<input type="number"  placeholder="请输入手机号"
					maxlength="11"
					v-model="phone"
					@blur="onBulePhone"
					placeholder-style="{
						width: 200px;
						font-size: 30rpx;
						font-family: PingFang SC;
						font-weight: 400;
						color: #fff;
					}"
					value="" />
					  <image :src="raomImg"
					  @click="updateRandImg"
					  v-if="phone.length===11"
                        class="ramom"></image>
				</view>
				<view class="pwd">
					<image src="/static/login/pwd.png" mode=""></image>
					<input type="text"  placeholder="请输入4位验证码"
					maxlength="4"
					v-model="imageCaptcha"
					placeholder-style="{
						width: 200px;
						font-size: 30rpx;
						font-family: PingFang SC;
						font-weight: 400;
						color: #fff;
					}"
					value="" />
				</view>
				<view class="pwd">
					<image src="/static/login/pwd.png" mode=""></image>
					<input type="text"  placeholder="请输入6-20位之间密码"
					v-model="pwd"
					maxlength="20"
					placeholder-style="{
						width: 200px;
						font-size: 30rpx;
						font-family: PingFang SC;
						font-weight: 400;
						color: #fff;
					}"
					value="" />
				</view>
				<view class="other">
					<text @tap="nextPage(`/pages/loginSms/loginSms`)">验证码登录</text>
<!--					<text @tap="nextPage(`/pages/loginForget/loginForget?type=big`)">忘记密码</text>-->
				</view>
				<view class="txt">
					<u-radio-group>
						<u-radio active-color="#2CBF7E"></u-radio>
					</u-radio-group>
					<text>我已阅读并同意<text class="deep">《用户协议》</text>和<text class="deep">《用户协议》</text></text>
				</view>
				<view class="btn" @click="onLogin">
					<button type="default">登录</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import indexBackgroundImage from "@/static/login/bg1.png"
	import{bizLogin,randomImage}from '@/api/login'
	import {isMPStrict} from '@/utils/utils'
	import { mapActions } from 'vuex'
	export default {
		data() {
			return {
				indexBackgroundImage: indexBackgroundImage,
				phone:'',//帐号  
				pwd:'',//密码 
				raomImg:'',//图片
				imageCaptcha:'',//验证码
			};
		},
		methods: {
			 ...mapActions(['setUserInfo','setUserToken']),
			 //验证码
			 onBulePhone(e){
				this.updateRandImg()
			 },
			 updateRandImg(){
				 this.imageCaptcha=''
				 if(this.phone.length===11&&isMPStrict(this.phone)){
				 					 randomImage(this.phone).then(res=>{
				 						this.raomImg=res.result							  
				 					 })
				 }
			 },
			 //登录
			onLogin(){
				if(!this.phone.length){
					  this.$showToast('请输入手机号码')
					return false
				}
				if(!isMPStrict(this.phone)){
				  this.$showToast('手机号码不合法')
				  return false
				}if(!this.imageCaptcha.length){
					this.$showToast('请输入验证码')
					return false
				} if(!this.pwd.length){
					this.$showToast('请输入密码')
					return false
				}
			
				const data={
					username:this.phone,
					password:this.pwd,
					imageCaptcha:this.imageCaptcha
				}
				this.$showLoading({
					title:'登录中...'
				})
				bizLogin(data).then(res=>{
					const {code, message, success,result:{
					  token,userInfo:user
					}} = res
					  if(code===200&&success){
						  this.$showToast(message)
						  this.setUserInfo({user})
						  this.setUserToken({token})
						  this.$u.route({
						    animationDuration:500,
						    url: '/pages/index/index',
						    type:'reLaunch'
						  })
					  }else{
						  this.$showToast(message)
						   this.imageCaptcha=''
						   this.updateRandImg()
					  }
				})
			},
			//跳转列表对应页面
			nextPage(url) {
				uni.navigateTo({
					url: url,
					fail() {
						uni.switchTab({
							url: url,
							fail() {
								uni.showToast({
									title: '暂未开通，敬请期待！',
									icon: 'none'
								});
							}
						});
					}
				});
			}
		}
	}
</script>

<style lang="scss">
.content{
	height: 100vh;
	background-repeat: no-repeat;
	background-size: 100% 100%;
	.main{
		padding: 0 30rpx;
		.title{
			padding-top: 120rpx;
			.h1{
				margin-bottom: 20rpx;
				text{
					font-size: 60rpx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #FFFFFF;
				}
			}
			.h2{
				margin-bottom: 60rpx;
				text{
					font-size: 38rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #FFFFFF;
				}
			}
		}
		.login{
			padding: 30rpx 30rpx 0 30rpx;
			height: 700rpx;
			border-radius: 50rpx;
			border: #B8C0CB 2rpx solid;
			box-shadow: 0 4rpx 12rpx 0 RGBA(182, 191, 199, 1);
			.tab{
				display: flex;
				justify-content: space-around;
				margin: 30rpx 0;
				.active{
					padding: 10rpx 40rpx;
					font-size: 34rpx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #62C2B5;
					background: #C9EBE4;
					border-radius: 40rpx;
				}
				text{
					padding: 10rpx 40rpx;
					font-size: 34rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #FFFFFF;
					border-radius: 40rpx;
				}
			}
			.tel{
				display: flex;
				align-items: center;
				margin: 60rpx 0 30rpx 0;
				padding: 20rpx;
				height: 100rpx;
				border-radius: 50rpx;
				border: #B8C0CB 2rpx solid;
				box-shadow: 0 4rpx 12rpx 0 RGBA(182, 191, 199, 1);
				image{
					width: 50rpx;
					height: 50rpx;
				}
				input{
					margin-left: 14rpx;
				}
				.ramom{
					width: 80rpx;
					    height: 80rpx;
					    position: absolute;
					    right: 95rpx;
				}
			}
			
			.pwd{
				display: flex;
				align-items: center;
				margin-bottom: 20rpx;
				padding: 20rpx;
				height: 100rpx;
				border-radius: 50rpx;
				border: #B8C0CB 2rpx solid;
				box-shadow: 0 4rpx 12rpx 0 RGBA(182, 191, 199, 1);
				image{
					width: 50rpx;
					height: 50rpx;
				}
				input{
					margin-left: 14rpx;
				}
			}
			.other{
				display: flex;
				justify-content: space-between;
				margin-bottom: 50rpx;
				padding: 0 20rpx;
				text{
					font-size: 21rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #FEFFFF;
				}
			}
			.txt{
				margin-bottom: 30rpx;
				text-align: center;
				text{
					font-size: 24rpx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #FFFFFF;
				}
				.deep{
					color: #83E9D7;
				}
			}
			.btn{
				button{
					background: #58BEAF;
					border-radius: 50rpx;
					color: #FFFFFF;
					font-weight: bold;
					font-size: 34rpx;
					letter-spacing: 5rpx;
				}
			}
		}
	}
}
</style>
